﻿using System;
using System.Collections.Generic;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;

namespace Gameplay.PVE.Survivor
{
    public class KnockBackComponent: ComponentBase
    {
        
        private float knockDistance;
        private float knockTime;
        private float knockStartTime;
        private float accelerate;
        private float speed;

        private bool isKnocking;
        private Vector3 knockDirection;
        public void SetKnock(float distance,float time,Vector3 direction)
        {
            accelerate = 2 * distance / time / time;
            knockStartTime = TimeManager.time;
            speed = accelerate * time;
            knockDistance = distance;
            knockTime = time;
            isKnocking = true;
            knockDirection = direction.normalized;
        }
        protected override void OnInitialized()
        {
            
        }

        protected override void OnUpdate()
        {
            if (isKnocking)
            {
                if (TimeManager.time - knockStartTime >= knockTime)
                {
                    isKnocking = false;
                }
                else
                {
                    speed -= accelerate * Time.deltaTime;
                    var targetPosition = unit.Position + knockDirection * speed * Time.deltaTime;
                    if (unit.isBoss)
                    {
                        //需要判断一下navmesh
                        unit.Position = BattleHelper.GetNavmeshValidPosition(targetPosition,0.1f);
                    }
                    else
                    {
                        unit.Position = targetPosition;
                    }
                }
            }
        }
        
        public bool IsKnocking()
        {
            return isKnocking;
        }

        public override void OnReset()
        {
            base.OnReset();
            isKnocking = false;
        }
    }
}
